package com.ma.common.config.exception;

import com.ma.common.result.Result;
import com.ma.common.result.ResultCodeEnum;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    //全局异常处理方法
    @ExceptionHandler(Exception.class)
    public Result error(){
        return Result.fail().message("全局异常处理....");
    }
    //特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    public Result error(ArithmeticException e){
        return Result.fail().message("执行特定异常处理");
    }
    //自定义异常处理
    @ExceptionHandler(MaException.class)
    public Result error(MaException e){
        return Result.fail().message(e.getMessage()).code(e.getCode());
    }

    //spring security异常
    @ExceptionHandler(AccessDeniedException.class)
    public Result error(AccessDeniedException e) throws AccessDeniedException {
        return Result.build(null, ResultCodeEnum.PERMISSION);
    }
}
